﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using BaoJianSoft.Common;
using BaoJianSoft.ExtensionModel.Statistic;
using BaoJianSoft.ExtensionModel.Web.Properties;
using BaoJianSoft.RapidWeb;
using BaoJianSoft.RapidWeb.DynamicPages;
using BaoJianSoft.RapidWeb.DynamicPages.Configurations;

namespace BaoJianSoft.ExtensionModel.Web.DynamicPage
{
	/// <summary>
	/// The callback to create grid column configurations of dynamic page for statistic report.
	/// There has to set a statistic definition into TempVariables as key "StatisticDefinition" in SetupContextTempVariables of IDynamicPage handler that the abstract dynamic page handler "StatisticReportDynamicPage" has implemented it already.
	/// </summary>
	public class StatisticReportGridColumnConfigurationProcessCallback : IDynamicPageConfigurationProcessCallback
	{
		private static IApplicationContext applicationContext = SpringContext.Current.GetObject<IApplicationContext>();

		/// <summary>
		/// Process the dynamic page configuration to add statistic grid columns at runtime.
		/// The method will resolve statistic definition from AuthenticationContext.TempVariables by the key "StatisticDefinition".
		/// </summary>
		/// <param name="configuration"></param>
		void IDynamicPageConfigurationProcessCallback.Process(DynamicPageConfiguration configuration)
		{
			this.Process(configuration);
		}

		/// <summary>
		/// Process the dynamic page configuration to add statistic grid columns at runtime.
		/// The method will resolve statistic definition from AuthenticationContext.TempVariables by the key "StatisticDefinition".
		/// </summary>
		/// <param name="configuration"></param>
		protected virtual void Process(DynamicPageConfiguration configuration)
		{
			StatisticDefinition statisticDefinition = applicationContext.TempVariables["StatisticDefinition"] as StatisticDefinition;
			if (statisticDefinition == null)
			{
				string errorMessage = string.Format("The StatisticReportGridColumnConfigurationProcessCallback configured in the dynamic page \"{0}\" cannot work without \"StatisticDefinition\" specified in TempVariables of IAuthenticationContext.", QueryStringUtility.ObjectId);
				Logger.Instance(this).Error(errorMessage);
				throw new InvalidProgramException(errorMessage);
			}

			GridViewPanelConfiguration gridViewPanelConfiguration = configuration.Panels.FirstOrDefault(p => p.PanelType == DynamicPagePanelTypes.GridViewPanel) as GridViewPanelConfiguration;
			gridViewPanelConfiguration.PrimaryKeyFieldName = "Name";
			gridViewPanelConfiguration.Fields = new Collection<GridViewFieldConfiguration>();

			gridViewPanelConfiguration.Fields.Add(new GridViewFieldConfiguration(StatisticApi.ROW_HEADER_TEXT_PROPERTY_NAME, Resources.Item) { Sortable = false, Resizable = true, Css = "font-weight:bold;" });
			foreach (StatisticFilter columnStatisticFilter in statisticDefinition.Columns)
			{
				string propertyName = StatisticApi.GetStatisticPropertyName(columnStatisticFilter);
				GridViewFieldConfiguration gridViewFieldConfiguration = new GridViewFieldConfiguration(propertyName, columnStatisticFilter.HeaderText);
				gridViewFieldConfiguration.Align = HorizontalAlign.Center;
				gridViewFieldConfiguration.Sortable = false;
				gridViewFieldConfiguration.Resizable = false;
				gridViewPanelConfiguration.Fields.Add(gridViewFieldConfiguration);
			}

			gridViewPanelConfiguration.Fields.Add(new GridViewFieldConfiguration(StatisticApi.STATISTIC_PROPERTY_NAME, Resources.Summary) { Sortable = false, Resizable = false, Align = HorizontalAlign.Center });
		}
	}
}

